﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DavidCms.Web.Models;

namespace DavidCms.Web.Controllers
{
    public class ArticleBgController : Controller
    {
        private CmsContext db = new CmsContext();

        //
        // GET: /ArticleBg/

        public ActionResult Index()
        {
            return View(db.Article.ToList());
        }

        //
        // GET: /ArticleBg/Details/5

        public ActionResult Details(int id = 0)
        {
            ArticlePO articlepo = db.Article.Find(id);
            if (articlepo == null)
            {
                return HttpNotFound();
            }
            return View(articlepo);
        }

        //
        // GET: /ArticleBg/Create

        public ActionResult Create()
        {
            var query = from p in db.Navigation
                        select p;
            List<SelectListItem> navigationList = new List<SelectListItem>();
            foreach (var p in query.ToList())
            {
                navigationList.Add(new SelectListItem
                {
                    Text = p.Name,
                    Value = p.Id.ToString(),
                });

            }
            ViewBag.NavigationId = navigationList;
            return View();
        }

        //
        // POST: /ArticleBg/Create

        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ArticlePO articlepo)
        {
            if (ModelState.IsValid)
            {
                articlepo.CreateTime = DateTime.Now;
                articlepo.publishTime = DateTime.Now;
                db.Article.Add(articlepo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(articlepo);
        }

        //
        // GET: /ArticleBg/Edit/5

        public ActionResult Edit(int id = 0)
        {
            var query = from p in db.Navigation
                        select p;
            List<SelectListItem> navigationList = new List<SelectListItem>();

            ArticlePO articlepo = db.Article.Find(id);
            if (articlepo == null)
            {
                return HttpNotFound();
            }

            foreach (var p in query.ToList())
            {
                navigationList.Add(new SelectListItem
                {
                    Text = p.Name,
                    Value = p.Id.ToString(),
                    Selected = (p.Id == articlepo.NavigationId),
                });

            }
            ViewBag.NavigationId = navigationList;
            return View(articlepo);
        }

        //
        // POST: /ArticleBg/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ArticlePO articlepo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(articlepo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(articlepo);
        }

        //
        // GET: /ArticleBg/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ArticlePO articlepo = db.Article.Find(id);
            if (articlepo == null)
            {
                return HttpNotFound();
            }
            return View(articlepo);
        }

        //
        // POST: /ArticleBg/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ArticlePO articlepo = db.Article.Find(id);
            db.Article.Remove(articlepo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}